|
In computer science, the reentrant mutex (recursive mutex, recursive lock) is particular type of mutual exclusion (mutex) device that may be locked multiple times by the same process/thread, without causing a deadlock. While any attempt to perform the "lock" operation on an ordinary mutex (lock) would either fail or block when the mutex is already locked, on a recursive mutex this operation will succeed if and only if the locking thread is the one that already holds the lock. Typically, a recursive mutex tracks the number of times it has been locked, and requires equally many unlock operations to be performed before other threads may lock it. ==Motivation== Recursive mutexes solve the problem of non-reentrancy with regular mutexes: if a function that takes a lock and executes a callback is itself called by the callback, deadlock ensues. In pseudocode, that is the following situation: var m : Mutex // A non-recursive mutex, initially unlocked. function lock_and_call(i : Integer) m.lock() callback(i) m.unlock() function callback(i : Integer) if i > 0 lock_and_call(i - 1) Given these definitions, the function call will cause the following sequence of events: * — mutex locked * * — because * — deadlock, because is already locked, so the executing thread will block, waiting for itself. Replacing the mutex with a recursive one solves the problem, because the final will succeed without blocking. 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「reentrant mutex」の詳細全文を読む スポンサード リンク
|